package com.guigu.servicesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.servicesystem.entity.TbLoginLog;
import com.guigu.servicesystem.entity.TbMenu;
import com.guigu.servicesystem.service.TbMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/servicesystem/menu")
public class TbMenuController {
    //注入
    @Autowired
    private TbMenuService tbMenuService;


    //查询所有
    @GetMapping("findAll")
    public Result findAllTbTbMenu(){
        List<TbMenu> list = tbMenuService.list(null);
        return  Result.ok().data("items",list);
    }


    //条件+分页查询数据
    @PostMapping("findPage")
    public Result findPage(@RequestBody Map<String, Object> searchMap,Integer page,Integer size) {
        return tbMenuService.findPage(searchMap, page, size);
    }

    //根据id查询
    @GetMapping("findById")
    public Result getTbTbMenu(String id) {
        TbMenu tbMenu = tbMenuService.getById(id);
        return Result.ok().data("tbMenu",tbMenu);
    }

    //添加接口的方法
    @PostMapping("add")
    public Result addTbTbMenu(@RequestBody TbMenu tbMenu) {
        boolean save = tbMenuService.save(tbMenu);
        if(save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    //修改功能
    @PostMapping("update")
    public Result updateTbAdmin(@RequestBody TbMenu tbMenu){
        boolean update = tbMenuService.updateById(tbMenu);
        if (update) {
            return Result.ok();
        }
        return Result.error();
    }
    //逻辑删除
    @DeleteMapping("delete")
    public Result removeTbMenu(String id) {
        boolean flag = tbMenuService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
    @GetMapping("/findMenu")
    public Result findMenu() {
        List allMenu = tbMenuService.findMenuByParentId();
        System.out.println(allMenu);
        return Result.ok().data("data",allMenu);
    }


}

